package model

import (
	"context"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/schema"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type AdminUserModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewAdminUserModel(ctx context.Context, db *gorm.DB) *AdminUserModel {
	return &AdminUserModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *AdminUserModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *AdminUserModel) GetAdminUserByAccount(account string) (adminUser schema.AdminUser, err error) {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("account =?", account).First(&adminUser)
	if err := dbRes.Error; err != nil && err != gorm.ErrRecordNotFound {
		logc.Error(model.ctx, "AdminUserModel-GetAdminUserByAccount,err:", err)
		return adminUser, err
	}

	return adminUser, nil
}

func (model *AdminUserModel) UpdateAdminUser(adminUser schema.AdminUser) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id =?", adminUser.Id).Updates(&adminUser)
	if err := dbRes.Error; err != nil {
		logc.Error(model.ctx, "AdminUserModel-UpdateAdminUser,err:", err)
		return err
	}

	return nil
}

func (model *AdminUserModel) Logout(userIds ...int64) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id in ?", userIds).Updates(map[string]interface{}{
		"login_status": 0,
	})
	if err := dbRes.Error; err != nil {
		return fmt.Errorf("logout error: %v", err)
	}

	return nil
}

func (model *AdminUserModel) UpdateAdminUserPassword(userId int64, password string) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id=?", userId).Update("password", password)
	if err := dbRes.Error; err != nil {
		logc.Error(model.ctx, "AdminUserModel-UpdateAdminUserPassword,err:", err)
		return err
	}

	return nil
}

func (model *AdminUserModel) GetAdminUserPasswordByUserId(userId int64) (password string, err error) {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Select("password").Where("id =?", userId).First(&password)
	if err := dbRes.Error; err != nil && err != gorm.ErrRecordNotFound {
		logc.Error(model.ctx, "AdminUserModel-GetAdminUserPasswordByUserId,err:", err)
		return password, err
	}

	return password, nil
}

func (model *AdminUserModel) InsertSchema(data *schema.AdminUser) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Create(data)
	if err := dbRes.Error; err != nil {
		logc.Error(model.ctx, "AdminUserModel-InsertSchema,err:", err)
		return err
	}

	return nil
}

func (model *AdminUserModel) UpdateUserGoogleCode(userId int64, googleCode string) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id=?", userId).Update("google_code", googleCode)
	if err := dbRes.Error; err != nil {
		fmt.Println("AdminUserModel-UpdateUserPassword,err:", err)
		return err
	}

	return nil
}

func (model *AdminUserModel) ResetGoogleCode(userId int64) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id=?", userId).Update("google_code", "")
	if err := dbRes.Error; err != nil {
		fmt.Println("AdminUserModel-ResetGoogleCode,err:", err)
		return err
	}

	return nil
}

func (model *AdminUserModel) UpdateAdminUserInfo(adminUser schema.AdminUser) error {
	dbRes := model.getDb().Model(&schema.AdminUser{}).Where("id =?", adminUser.Id).Updates(map[string]interface{}{
		"account":          adminUser.Account,
		"password":         adminUser.Password,
		"nickname":         adminUser.Nickname,
		"role":             adminUser.Role,
		"head_picture_url": adminUser.HeadPictureUrl,
		"user_status":      adminUser.UserStatus,
	})
	if err := dbRes.Error; err != nil {
		logc.Error(model.ctx, "AdminUserModel-UpdateAdminUserInfo,err:", err)
		return err
	}

	return nil
}
